package com.notice.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DbHelper extends SQLiteOpenHelper{
	
	
	private static final String DB_NAME = "course_db";
	private static final int    VERSION = 1;
	
	private static final String TABLE_NAME = "course_table";
	private static final String COURSE_ID = "_id";
	private static final String WEEKDAY = "weekday";
	private static final String COURSE_NAME = "name";
	private static final String START_COUNT = "startc";
	private static final String END_COUNT = "endc";
	private static final String ISNOTIFY = "isnotify";
	private static final String PRETIME = "pretime";
				

	public DbHelper(Context context) {
		super(context, DB_NAME, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table "+TABLE_NAME+" ("
				+COURSE_ID + " integer primary key autoincrement, "
				+WEEKDAY + " text, "
				+COURSE_NAME + " text ,"
				+START_COUNT + " text ,"
				+ISNOTIFY + " text ,"
				+PRETIME + " text ,"
				+END_COUNT+" text )";
				db.execSQL(sql);	
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		String sql = "drop table if exists "+TABLE_NAME;
		db.execSQL(sql);	
	}
	
	public long insertCourse(String weekday, String name,String startcount, String endCount,String isNotify, String pretime) {
		SQLiteDatabase db = this.getWritableDatabase();
		ContentValues cv = new ContentValues();
		cv.put(WEEKDAY, weekday);
		cv.put(COURSE_NAME, name);
		cv.put(START_COUNT, startcount);
		cv.put(END_COUNT, endCount);
		cv.put(PRETIME, pretime);
		return db.insert(TABLE_NAME, null, cv);
	}
	

	public Cursor queryCourse(String weekday) {
		SQLiteDatabase db = this.getReadableDatabase();
		String[] selection = { weekday };
		return db.query(TABLE_NAME, null, WEEKDAY+"=?", selection, null, null, null);
	}
	
	public void deleteCourse(long id) {
		SQLiteDatabase db = this.getWritableDatabase();
		String[] selection = {String.valueOf(id)};
		db.delete(TABLE_NAME, COURSE_ID+"=?", selection);
	}
	
	public void updateCourse(long id, String weekday, String name,String startcount, String endCount,String isNotify, String pretime) {
		SQLiteDatabase db = this.getWritableDatabase();
		String[] selection = {String.valueOf(id)};
		ContentValues cv = new ContentValues();
		cv.put(WEEKDAY, weekday);
		cv.put(COURSE_NAME, name);
		cv.put(START_COUNT, startcount);
		cv.put(END_COUNT, endCount);
		cv.put(PRETIME, pretime);
		
		db.update(TABLE_NAME, cv, COURSE_ID+"=?", selection);
	}
 }
